package thinkinjava4.ch12_exceptions;

class BaseballException extends Exception { }
class Foul extends BaseballException { }
class Strike extends BaseballException { }

abstract class Inning {
    public Inning() throws BaseballException { }
    public void event() throws BaseballException { }
    public abstract void atBat() throws Strike, Foul;
    public void walk() { }
}

class StormException extends Exception { }
class RainedOut extends StormException { }
class PopFoul extends Foul { }

interface Storm{
    void event() throws RainedOut;
    void rainHard() throws RainedOut;
}

public class StormyInning extends Inning implements Storm{
    public StormyInning() throws RainedOut,BaseballException { }
    public StormyInning(String s) throws Foul,BaseballException{ }

    //抽象类方法没有定义throws异常，实现类方法不能抛出异常
//    @Override public void walk() throws PopFoul{ }

    //接口方法和抽象方法的throws 不同时，不能抛出异常，如果其中一个是子类，可以抛出子类异常
//    @Override public void event() throws RainedOut{ }

    /**
     * 实现类
     * @throws RainedOut
     */
    @Override
    public void rainHard() throws RainedOut { }

    @Override
    public void event() { }

    /**
     * 可以throws Strike, Foul的子类
     * @throws PopFoul
     */
    @Override
    public void atBat() throws PopFoul {
        System.out.println("atBat");
    }

    public static void main(String[] args) {
        try {
            StormyInning si=new StormyInning();
            si.atBat();
        }catch (PopFoul e){
            System.out.println("Pop foul");
        }catch (RainedOut e){
            System.out.println("Rained out");
        }catch (BaseballException e){
            System.out.println("Generic baseball exception");
        }

        try{
            Inning i=new StormyInning();
            i.atBat();
        }catch (Strike e){
            System.out.println("Strike");
        }catch (Foul e){
            System.out.println("Foul");
        }catch(RainedOut e){
            System.out.println("Rained out");
        }catch (BaseballException e){
            System.out.println("Generic baseball exception");
        }
    }
}
